<template>
  <div class="user-data">
    <el-page-header @back="goBack" content="修改资料"></el-page-header>
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" :rules="rules" class="demo-ruleForm">
      <!-- <el-form-item label="用户名">
        <el-input v-model="ruleForm.name" placeholder="请输入用户名"></el-input>
      </el-form-item> -->
      <el-form-item label="邮箱" prop="email">
		    <el-input v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="工作邮箱" v-if="ruleForm.role_id !==5">
        <el-input v-model="ruleForm.email_work" placeholder="请输入工作邮箱"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-no-more-click:3000 type="primary" @click="submitForm('ruleForm')">修改</el-button>
        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import aes from '~/util/aes.js'
export default {
  name: "userData",
  data() {
    return {
      ruleForm: {
        
      },
      rules: {
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      }
    };
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    resetForm(formName) {
      this.getUserInfo();
    },
    getUserInfo() {
      this.ruleForm = this.$store.state.user;
    },
    //提交修改
    submitForm(formName){
      this.$refs[formName].validate((valid) => { 
        if (valid) { 
          this.$axios.put('/api/consumer/update',{
            //name:this.ruleForm.name,
            email:this.ruleForm.email,
            email_work:this.ruleForm.email_work
          })
          .then(res => {
            if(res.data.code == 200){
              this.$message.success('修改成功');
              //修改成功后更新本地存储的数据
              var obj = {};
              obj = this.$store.state.user;
              obj.name = this.ruleForm.name;
              obj.email = this.ruleForm.email;
              obj.email_work = this.ruleForm.email_work;
              this.$cookies.set('user',aes.Encrypt(JSON.stringify(obj)));
              this.$store.commit("setUser",obj);
              this.$router.replace({
                path: "/user/myOrder/orderInProcess"
              })
            }
          })
        } else {
          return false
        }
        
      })
    }
  }
};
</script>
<style lang="less">
.user-data {
  .el-form {
    width: 800px;
    margin: 80px 0 0 50px;
		.el-input {
		  width: 300px;
		}
  }
}
</style>